//
//  XQAccountOtherCell.m
//  JoinPeso
//
//  Created by 小琦 on 2024/11/27.
//

#import "XQAccountOtherCell.h"
#import "XQCustomOptionalPop.h"

@interface XQAccountOtherCell()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@end

@implementation XQAccountOtherCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if(self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = kCardColor;
        [self makeUI];
        [self makeConstraints];
    }
    return self;
}

- (void)makeUI {
    [self.contentView addSubview:self.tableView];
    
}

- (void)makeConstraints {
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsZero);
    }];
}

#pragma mark - setter

- (void)setDataSource:(NSArray *)dataSource {
    _dataSource = dataSource;
    
    for (NSDictionary *dic in dataSource) {
        NSString *key = dic[@"concept"];
        NSString *value = dic[@"maneuvering"];
        if (key.length && value.length) {
            [self.dataDic setValue:value forKey:key];
        }
    }
    
    
    
    [self.tableView reloadData];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dic = self.dataSource[indexPath.section];
    NSString *concept = dic[@"concept"];
    
    XQCustomInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:@"XQCustomInfoCell" forIndexPath:indexPath];
    [cell setPaper:dic[@"paper"]];
    [cell setPlaceholder:dic[@"document"]];
    [cell setContent:self.dataDic[concept]];
    [cell setIsNumberKeyboard:[dic[@"natural"] integerValue] == 1];
    @weakify(self);
    cell.buttonHandle = ^{
        @strongify(self);
        self.endEditingHandle();
        [self didSelectAtIndexPath:indexPath];
    };
    cell.textFielHandle = ^(NSString * _Nonnull text) {
        @strongify(self);
        [self textFiledWithTextAtIndexPath:indexPath content:text];
    };
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 54;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    NSDictionary *dic = self.dataSource[section];
    
    XQTableHeaderView *headerView = [[XQTableHeaderView alloc] init];
    [headerView setContent:dic[@"won"]];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 32;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 0.01;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (self.endEditingHandle) {
        self.endEditingHandle();
    }
}

#pragma mark - private

- (void)didSelectAtIndexPath:(NSIndexPath *)indexPath {
    
    NSDictionary *dic = self.dataSource[indexPath.section];
    
    NSString *concept = dic[@"concept"];
    NSString *won = dic[@"won"];
    NSArray *array = dic[@"emissions"];
    
    NSMutableArray *totalArray = [NSMutableArray array];
    for (NSDictionary *dic in array) {
        NSString *text = dic[@"chance"];
        if (text.length) {
            [totalArray addObject:text];
        }
    }
    
    NSString *value = self.dataDic[concept];
    
    XQCustomOptionalPop *popView = [[XQCustomOptionalPop alloc] initWithTitle:won datas:totalArray];
    if (value.length) {
        [popView setSelctData:value];
    }else {
        [popView setSelctData:totalArray.firstObject];
    }
    
    XQPopupAnimView *anim = [[XQPopupAnimView alloc] initWithAnimationType:XQPopupAnimationCenter customView:popView];
    anim.needCover = YES;
    anim.makeCustomConstraints = ^(XQPopupAnimView *animView, UIView *customView) {
        [customView mas_remakeConstraints:^(MASConstraintMaker *make) {
            (void)make.center;
        }];
    };
    
    [anim showInView:[XQToolManager appRootViewController].view completion:^{}];
    
    @weakify(anim)
    popView.confimHandle = ^(NSString * _Nonnull data) {
        @strongify(anim);
        self.dataDic[concept] = data;
        NSIndexSet *indexSet = [NSIndexSet indexSetWithIndex:indexPath.section];
            [self.tableView reloadSections:indexSet withRowAnimation:UITableViewRowAnimationAutomatic];
        [anim hideAndCompletion:^{}];
    };
}

- (void)textFiledWithTextAtIndexPath:(NSIndexPath *)indexPath content:(NSString *)content{
    NSDictionary *dic = self.dataSource[indexPath.section];
    
    NSString *concept = dic[@"concept"];
    
    self.dataDic[concept] = content;
}



#pragma mark - lazy

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        _tableView.layer.cornerRadius = 15;
        _tableView.backgroundColor = [UIColor whiteColor];
        [_tableView setContentInset:UIEdgeInsetsMake(20, 0, 0, 0)];
        [_tableView setScrollEnabled:NO];
        _tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        [_tableView registerClass:[XQCustomInfoCell class] forCellReuseIdentifier:@"XQCustomInfoCell"];
    }
    return _tableView;
}

- (NSMutableDictionary *)dataDic {
    if (!_dataDic) {
        _dataDic = [NSMutableDictionary dictionary];
    }
    return _dataDic;
}

@end
